<?php
/**
 * a b c d e f g h i j k l m n o p q r s t u v w x y z
 * 0 1 2 3 4 5 6 7 8 9 
 */
class Injeen {

	private			$version	= "0.1";
	private static	$imports	= array();
	private static	$config		= null;
	private			$isHTML		= true;

	function __construct($args) {

		Injeen::import("FrameworkException");
		Injeen::import("Config");
		Injeen::import("Request");
		Injeen::import("I18n");
		Injeen::import("Log");

		/*$log				=*/new Log;

		$config				= new Config($args);
		$config->version	= $this->version;
		self::$config		= $config;

		$i18n				= new I18n;
		$i18n->addFile("i18n/%LOCAL/framework/core.xml");

		$request			= new Request;
		$request			= $request->getArguments();
		
		if ($config->gzip === true) {
			if (strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")) {
				$config->isGzipped = true;
				ob_start(array(__CLASS__, "gzipOutput"));
			}
		}

		header(sprintf("X-Powered-By-Framework: Injeen PHP/%s", $this->version));

		$file = sprintf($config->classSchema, $config->path, ucfirst($config->mainClass));
		if (file_exists($file) === false) {
			$file = sprintf($config->classSchema, $config->path, $config->mainClass);
			if (file_exists($file) === false) {
				Log::write($i18n->getText("file_not_found", $file));
			}
		} else {
			$config->mainClass = ucfirst($config->mainClass);
		}

		include_once($file);

		if (class_exists($config->mainClass) === false) {
			Log::write($i18n->getText("class_not_found", $config->mainClass, $file));
		} else {
			$classStartTime		= microtime(true);
			$classMemoryUsage	= memory_get_usage(); 
			$object				= new $config->mainClass; #don't get confused
		}
	
		if (method_exists($config->mainClass, $config->mainMethod) === false) {
			Log::write($i18n->getText("method_not_found", $config->mainMethod, $config->mainClass));
		} else {
			$callback = new ReflectionMethod($config->mainClass, $config->mainMethod);
			$callback->invokeArgs($object, $request);
		}

		Log::write($i18n->getText("execute_time_of", $config->mainClass, $config->mainMethod, (microtime(true) - $classStartTime)));
		Log::write($i18n->getText("memory_usage", (memory_get_usage() - $classMemoryUsage)));
		Log::write($i18n->getText("framework_execute_time_is", (microtime(true) - $config->start)));
		Log::write($i18n->getText("respond_time_is", (microtime(true) - $_SERVER['REQUEST_TIME'])));

		if ($object) {
			unset($object);			
		}

		if ($config->isGzipped === true) {
			ob_flush();
		}

	}
	
	private static function gzipOutput($output) {
		if (strlen($output) >= 1000) {
			header("Content-Encoding: gzip");
			return gzencode($output);
		} else {
			return $output;
		}
	}

	public static function import($library) {

		$config = self::$config; # believe or not

		if (in_array($library, self::$imports)) {
			return;
		} else {
			self::$imports[] = $library;
		}

		$core 	= array("Config", "FrameworkException", "I18n", "Json", "Log", "Store", "Image", "URL",
						"Mail", "Debug", "Node", "Request", "Selector", "View", "XPath", "Memcached", "Form");
		
		if (in_array($library, $core)) {
			#config class is not yet impoted so I can't get phpExt value
			return include_once(sprintf("%s.php", $library));
		} else if (is_array($config->dbTables) && in_array($library, $config->dbTables)) {
			self::importModel($library);
		} else {
			#assert that Config is already imported
			$config = new Config;
			return include_once(sprintf("%s%s%s", $config->classDir, $library, $config->phpExt));
		}
	}

	private static function importModel($class) {
		$config = new Config;
		switch (strtolower($config->dbType)) {
			case "mysql":
				include_once("DBDrivers/mysql.php");
			break;
		}
		include_once("Model.php");
		include_once("DB.php");
		$key = null;
		$table = null;
		foreach ($config->dbTables as $k => $v) {
			if ($v == $class) {
				$key = $k;
				break;
			}
		}
		$syntax = 'class %s extends Model {'
				. 'public function __construct($local = null) {'
				. 'parent::__construct("%s", $local);}}';
		eval(sprintf($syntax, $class, $key));
	}

}
